/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package state;

import java.util.Collection;
import java.util.HashMap;
import static snazzysprites.SnazzySpritesSettings.*;

/**
 * A 2D character or object consisting of various animations
 * @author Andrew Gratta <andrew.gratta@stonybrook.edu>
 */
public class SpriteType {
    //String name;
    HashMap<String, Animation> animations;
    
    /**
     * Creates a new sprite type with the given name, and adds a default animation
     * @param name 
     */
    public SpriteType()
    {
       animations = new HashMap<String, Animation>();
       Animation initialAnimation = new Animation("New Animation", 
               DEFAULT_ANIMATION_WIDTH, DEFAULT_ANIMATION_HEIGHT, true);
    }
    
    /**
     * Add an animation the sprite type's list of animations
     * @param anim 
     */
    public void addAnimation(Animation anim)
    {
        animations.put(anim.getName(), anim);
    }
    
    /**
     * Renames one of the sprite type's animations
     * @param oldName The name of the animation to rename
     * @param newName The new name for the animation
     */
    public void renameAnimation(String oldName, String newName)
    {
        if (animations.get(newName) != null)
        {
            //We can't have more than one animation with the same name
            return;
        }
        Animation anim = animations.remove(oldName);
        if (anim != null)
        {
            anim.setName(newName);
            animations.put(newName, anim);
        }
    }
    
    /**
     * Get an animation by name
     * @param animationName
     * @return Returns the desired animation if it exists. Otherwise null will be returned.
     */
    public Animation getAnimation(String animationName)
    {
        return animations.get(animationName);
    }
    
    /**
     * Removes the specified animation from the SpriteType
     * @param animationName 
     */
    public void deleteAnimation(String animationName)
    {
        Animation anim = animations.remove(animationName);
        if (animations.size() < 1 && anim != null)
        {
            Animation defaultAnim = new Animation(DEFAULT_ANIMATION_NAME, 
                    DEFAULT_ANIMATION_WIDTH, DEFAULT_ANIMATION_HEIGHT, true);
            animations.put(DEFAULT_ANIMATION_NAME, anim);
        }
    }
    
    public Collection<Animation> getAnimationList()
    {
        return animations.values();
    }
    
//    /**
//     * Get the name of this sprite
//     * @return 
//     */
//    public String getName()
//    {
//        return name;
//    }
    
    /**
     * Get the number of animations this sprite type contains
     * @return 
     */
    public int getAnimationCount()
    {
        return animations.size();
    }
    
    public int getMaxAnimationWidth()
    {
        int width = 0;
        for (Animation anim : animations.values())
        {
            if (anim.getWidth() > width)
                width = anim.getWidth();
        }
        return width;
    }
    
    public int getMaxAnimationHeight()
    {
        int height = 0;
        for (Animation anim : animations.values())
        {
            if (anim.getHeight() > height)
                height = anim.getHeight();
        }
        return height;
    }
    
    /**
     * Get the names of the sprite type's animations
     * @return 
     */
    public String[] getAnimationNames()
    {
        return animations.keySet().toArray(new String[0]);
    }
}
